/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package elaborate.vwbe.impl;

import elaborate.vwbe.impl.scripting.DefaultJavaScriptEngine;
import elaborate.vwbe.scripting.ScriptEngine;
import elaborate.vwbe.scripting.ScriptEngineContext;
import elaborate.vwbe.scripting.ScriptEngineManager;
import elaborate.vwbe.scripting.ScriptLanguage;
import elaborate.vwbe.scripting.ScriptingException;
import org.w3c.dom.Document;

/**
 *
 * @author lendle
 */
public class DefaultScriptEngineManager implements ScriptEngineManager{

    protected Document document;

    /**
     * Get the value of document
     *
     * @return the value of document
     */
    public Document getDocument() {
        return document;
    }

    /**
     * Set the value of document
     *
     * @param document new value of document
     */
    public void setDocument(Document document) {
        this.document = document;
    }

    public ScriptEngine createScriptEngine(ScriptLanguage language) throws ScriptingException {
        if(language.equals(ScriptLanguage.JavaScript)){
            DefaultJavaScriptEngine engine=new DefaultJavaScriptEngine();
            ScriptEngineContext context=new DefaultScriptEngineContext();
            engine.setDocument(document);
            engine.setContext(context);
            javax.script.ScriptEngineManager factory = new javax.script.ScriptEngineManager();
            javax.script.ScriptEngine _engine=factory.getEngineByName("JavaScript");
            _engine.put("document", this.document);
            _engine.put("application", context);
            engine.set_engine(_engine);
            return engine;
        }
        return null;
    }

}
